<?php
// 放在插件目录下通过浏览器访问

echo '<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>防火墙日志系统诊断</title>
    <link href="https://fastly.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <style>
        .card { margin-bottom: 20px; }
        .badge { font-size: 0.9rem; }
        .header { 
            background-color: #f8f9fa; 
            padding: 20px 0; 
            margin-bottom: 30px;
            cursor: pointer;
            transition: background-color 0.3s;
        }
        .header:hover {
            background-color: #e9ecef;
        }
        .collapse-content {
            display: none;
        }
        #toggleIcon {
            transition: transform 0.3s;
        }
        .rotated {
            transform: rotate(180deg);
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="header text-center" onclick="toggleContent()">
            <h1 class="display-4">
                防火墙日志系统诊断
                <svg id="toggleIcon" xmlns="http://www.w3.org/2000/svg" width="24" height="24" fill="currentColor" class="bi bi-chevron-down" viewBox="0 0 16 16">
                    <path fill-rule="evenodd" d="M1.646 4.646a.5.5 0 0 1 .708 0L8 10.293l5.646-5.647a.5.5 0 0 1 .708.708l-6 6a.5.5 0 0 1-.708 0l-6-6a.5.5 0 0 1 0-.708z"/>
                </svg>
            </h1>
            <p class="lead">检查系统目录和文件权限状态</p>
        </div>

        <div id="contentArea" class="collapse-content">';

$dirs = [
    '插件根目录' => __DIR__,
    '日志目录' => __DIR__ . '/logs',
    '数据目录' => __DIR__ . '/data'
];

foreach ($dirs as $name => $dir) {
    echo '
        <div class="card">
            <h5 class="card-header">'.$name.' <small class="text-muted">'.$dir.'</small></h5>
            <div class="card-body">
                <ul class="list-group list-group-flush">
                    <li class="list-group-item d-flex justify-content-between align-items-center">
                        <span>是否存在</span>
                        <span class="badge rounded-pill '. (file_exists($dir) ? 'bg-success' : 'bg-danger') .'">'. (file_exists($dir) ? '是' : '否') .'</span>
                    </li>';
    
    if (file_exists($dir)) {
        echo '
                    <li class="list-group-item d-flex justify-content-between align-items-center">
                        <span>可读</span>
                        <span class="badge rounded-pill '. (is_readable($dir) ? 'bg-success' : 'bg-danger') .'">'. (is_readable($dir) ? '是' : '否') .'</span>
                    </li>
                    <li class="list-group-item d-flex justify-content-between align-items-center">
                        <span>可写</span>
                        <span class="badge rounded-pill '. (is_writable($dir) ? 'bg-success' : 'bg-danger') .'">'. (is_writable($dir) ? '是' : '否') .'</span>
                    </li>
                    <li class="list-group-item d-flex justify-content-between align-items-center">
                        <span>权限</span>
                        <span class="badge rounded-pill bg-primary">'. substr(sprintf('%o', fileperms($dir)), -4) .'</span>
                    </li>';
    }
    echo '
                </ul>
            </div>
        </div>';
}

$files = [
    '访问日志' => __DIR__ . '/logs/access.log',
    '拦截日志' => __DIR__ . '/logs/blocked.log',
    '黑名单' => __DIR__ . '/data/blacklist.txt',
    '白名单' => __DIR__ . '/data/whitelist.txt'
];

foreach ($files as $name => $file) {
    echo '
        <div class="card">
            <h5 class="card-header">'.$name.' <small class="text-muted">'.$file.'</small></h5>
            <div class="card-body">
                <ul class="list-group list-group-flush">
                    <li class="list-group-item d-flex justify-content-between align-items-center">
                        <span>是否存在</span>
                        <span class="badge rounded-pill '. (file_exists($file) ? 'bg-success' : 'bg-danger') .'">'. (file_exists($file) ? '是' : '否') .'</span>
                    </li>';
    
    if (file_exists($file)) {
        echo '
                    <li class="list-group-item d-flex justify-content-between align-items-center">
                        <span>可读</span>
                        <span class="badge rounded-pill '. (is_readable($file) ? 'bg-success' : 'bg-danger') .'">'. (is_readable($file) ? '是' : '否') .'</span>
                    </li>
                    <li class="list-group-item d-flex justify-content-between align-items-center">
                        <span>可写</span>
                        <span class="badge rounded-pill '. (is_writable($file) ? 'bg-success' : 'bg-danger') .'">'. (is_writable($file) ? '是' : '否') .'</span>
                    </li>
                    <li class="list-group-item d-flex justify-content-between align-items-center">
                        <span>文件大小</span>
                        <span class="badge rounded-pill bg-info">'. formatSize(filesize($file)) .'</span>
                    </li>';
    }
    echo '
                </ul>
            </div>
        </div>';
}

function formatSize($bytes) {
    if ($bytes >= 1073741824) {
        return number_format($bytes / 1073741824, 2) . ' GB';
    } elseif ($bytes >= 1048576) {
        return number_format($bytes / 1048576, 2) . ' MB';
    } elseif ($bytes >= 1024) {
        return number_format($bytes / 1024, 2) . ' KB';
    } elseif ($bytes > 1) {
        return $bytes . ' bytes';
    } elseif ($bytes == 1) {
        return '1 byte';
    } else {
        return '0 bytes';
    }
}

echo '
        </div>
    </div>

    <script>
        function toggleContent() {
            const content = document.getElementById("contentArea");
            const icon = document.getElementById("toggleIcon");
            
            if (content.style.display === "none") {
                content.style.display = "block";
                icon.classList.add("rotated");
            } else {
                content.style.display = "none";
                icon.classList.remove("rotated");
            }
        }
        
        // 默认隐藏内容
        document.addEventListener("DOMContentLoaded", function() {
            document.getElementById("contentArea").style.display = "none";
        });
    </script>
    
    <script src="https://fastly.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>';
